﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using StockTrader.DAL.IDAL;
using StockTrader.DAL.MSSQLHelper;
using StockTrader.BusinessService.DataContract;

namespace StockTrader.DAL.SQLserver
{
    public class Indicator : IIndicator
    {
/*
        private SqlConnection _internalConnection;
        private SqlTransaction _internalADOTransaction = null;

        public void BeginADOTransaction()
        {
            if (_internalConnection.State != System.Data.ConnectionState.Open)
                _internalConnection.Open();
            _internalADOTransaction = _internalConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
        }
        public void RollBackTransaction()
        {
            _internalADOTransaction.Rollback();
            _internalADOTransaction = null;
        }
        public void CommitADOTransaction()
        {
            _internalADOTransaction.Commit();
            _internalADOTransaction = null;
        }
        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new SqlConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        //SQL string
        private const string SQL_INDICATOR_INSERT = "INSERT INTO INDICATOR(DOCID, CODE, VALUE) VALUES (@DOCID, @CODE, @VALUE); SELECT ID=@@IDENTITY;";
        private const string SQL_INDICATOR_DELETE = "DELETE FROM INDICATOR WHERE ID=@ID";
        private const string SQL_INDICATOR_UPDATE = "UPDATE INDICATOR SET DOCID=@DOCID, CODE=@CODE, VALUE=@VALUE WHERE ID=@ID";

        private const string SQL_INDICATOR_GETBYDOC = "SELECT * FROM V_INDICATOR WHERE ID=@ID";
        private const string SQL_INDICATOR_GETBYDOCANDCODE = "SELECT * FROM V_INDICATOR WHERE DOCID=@DOCID AND CODE=@CODE";

        #region Parameters
        private const string PARM_ID = "@ID";
        private const string PARM_DOCID = "@DOCID";
        private const string PARM_CODE = "@CODE";
        private const string PARM_VALUE = "@VALUE";
        #endregion


        #region IIndicator Members


        public int InsertIndicator(IndicatorData item)
        {
            try
            {
                List<IndicatorCodeUI> codeList = new List<IndicatorCodeUI>();
                SqlParameter[] parms = new SqlParameter[] 
                    {new SqlParameter(PARM_DOCID, SqlDbType.Int),
                     new SqlParameter(PARM_CODE, SqlDbType.NVarChar),
                     new SqlParameter(PARM_VALUE, SqlDbType.Decimal)};
                parms[0].Value = item.DocId;
                parms[1].Value = item.Code;
                parms[2].Value = item.Value;
                return SQLHelper.ExecuteScalar(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_INDICATOR_INSERT, parms);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateIndicator(IndicatorData item)
        {
            try
            {
                List<IndicatorCodeUI> codeList = new List<IndicatorCodeUI>();
                SqlParameter[] parms = new SqlParameter[] 
                    {new SqlParameter(PARM_DOCID, SqlDbType.Int),
                     new SqlParameter(PARM_CODE, SqlDbType.NVarChar),
                     new SqlParameter(PARM_VALUE, SqlDbType.Decimal)};
                parms[0].Value = item.DocId;
                parms[1].Value = item.Code;
                parms[2].Value = item.Value;
                SQLHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_INDICATOR_UPDATE, parms);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteIndicator(int id)
        {
            try
            {
                List<IndicatorUI> codeList = new List<IndicatorUI>();
                SqlParameter parm1 = new SqlParameter(PARM_ID, SqlDbType.Int);
                parm1.Value = id;
                SQLHelper.ExecuteNonQuerySingleParm(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_INDICATOR_DELETE, parm1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<IndicatorUI> GetByDoc(int docId)
        {
            try
            {
                List<IndicatorUI> list = new List<IndicatorUI>();
                List<IndicatorUI> codeList = new List<IndicatorUI>();
                SqlParameter parm1 = new SqlParameter(PARM_ID, SqlDbType.Int);
                parm1.Value = docId;
                SqlDataReader rdr = SQLHelper.ExecuteReaderSingleParm(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_INDICATOR_GETBYDOC, parm1);
                while (rdr.Read())
                {
                    IndicatorUI item = new IndicatorUI(Convert.ToInt32(rdr["DOCID"]), rdr["CODE"].ToString(), Convert.ToDecimal(rdr["VALUE"]));
                    list.Add(item);
                }
                rdr.Close();
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<IndicatorUI> GetByDocAndCode(int docId, string code)
        {
            try
            {
                List<IndicatorUI> list = new List<IndicatorUI>();
                List<IndicatorUI> codeList = new List<IndicatorUI>();
                SqlParameter[] parms = new SqlParameter[]{
                    new SqlParameter(PARM_ID, SqlDbType.Int),
                    new SqlParameter(PARM_CODE, SqlDbType.NVarChar)};
                parms[0].Value = docId;
                parms[1].Value = code.Trim();
                SqlDataReader rdr = SQLHelper.ExecuteReader(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_INDICATOR_GETBYDOCANDCODE, parms);
                while (rdr.Read())
                {
                    IndicatorUI item = new IndicatorUI(Convert.ToInt32(rdr["DOCID"]), rdr["CODE"].ToString(), Convert.ToDecimal(rdr["VALUE"]));
                    list.Add(item);
                }
                rdr.Close();
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
*/
    }
}
